package com.controller;

import com.dao.impl.FlightDaoImpl;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Program: exam
 * @ClassName: FlightChangeStatusServletController
 * @Description: ${description}
 * @Author: li
 * @Create: 2024-12-23 20:46
 */
@WebServlet("/FlightStatusServlet")
public class FlightChangeStatusServletController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应类型和编码
        response.setContentType("application/json;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        String fid = request.getParameter("fid");
        // 修改这里：从 fstatus 获取状态值
        String statusStr = request.getParameter("fstatus");

        Map<String,Object> map = new HashMap<>();
        try {
            if (fid == null || statusStr == null) {
                map.put("code", -1);
                map.put("msg", "参数错误");
            } else {
                int status = Integer.parseInt(statusStr);
                new FlightDaoImpl().changeFlightStatus(Integer.parseInt(fid), status);
                map.put("code", "0");
                map.put("msg", "success");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", -1);
            map.put("msg", "更新状态失败");
        }

        String jsonResult = JSONObject.fromObject(map).toString();
        System.out.println("响应结果：" + jsonResult);
        response.getWriter().write(jsonResult);
    }
}
